import re
import pandas


# 日期格式转换，加年月日星期，是否周末列
def time_split(df, date_column):
    df[date_column] = pd.to_datetime(df[date_column])
    df['year'] = df[date_column].dt.year
    df['month'] = df[date_column].dt.month
    df['day'] = df[date_column].dt.day
    df['week'] = df[date_column].dt.weekday + 1
    df['date'] = df[date_column].dt.date  # 不然存到excel里面还有小时分秒，看着太长

    def is_weekend(x):
        if x == 6 or x == 7:
            return 1
        else:
            return 0

    df['weekend'] = df['week'].map(lambda x: is_weekend(x))

# 中文年月日转换成连接符格式，将如1月2日转换成01/02 用re.compile+findall，提取月日
def date_chinese_switch(data,column_date):
    pattern_date = re.compile(r"\d{1,2}")  # 几月几日

    def month_day_trans(str):
        mouth_day = pattern_date.findall(str)
        if len(mouth_day[0]) == 1: mouth_day[0] = '0' + mouth_day[0]
        if len(mouth_day[1]) == 1: mouth_day[1] = '0' + mouth_day[1]
        return mouth_day[0] + '/' + mouth_day[1]

    data[column_date] = data[column_date].map(lambda x: month_day_trans(x))

# 根据当前的年月，推算去年年份、上月(季)月份、上月(季)年份，上月(季)同比的年份
# 用于确定时期，计算同比、环比、上月同比
def period_for_tonghuan(this_year,this_period,period):
    '''
    :param this_year: int 本年年份
    :param this_period: int 本月或季度的数字
    :param period: str 'month'or'season'
    :return: 去年年份、上月(季)月份、上月(季)年份，上月(季)同比的年份
    '''
    last_year = this_year - 1  # 去年int
    if this_period != 1:
        last_period = this_period - 1
        last_period_this_year = this_year
        last_period_last_year = last_year
    else:
        if period == 'month':
            last_period = 12
        if period == 'season':
            last_period = 4
            last_period_this_year = this_year - 1
            last_period_last_year = last_year - 1

    return [last_year,last_period,last_period_this_year,last_period_last_year]

# 推算去年下期与下下期的年与月，做为未来两个月数据走势的参考
# 返回：下期月份、下下期月份、下期去年的年份，下下期去年的年份
def period_for_reference(this_year,this_period,period):
    '''
       :param this_year: int 本年年份
       :param this_period: int 本月或季度的数字
       :param period: str 'month'or'season'
       :return: 下期月份、下下期月份、下期去年的年份，下下期去年的年份
    '''
    last_year = this_year - 1  # 去年int
    if period == 'month':
        if this_period != 12:
            next_period = this_period + 1
            next_period_last_year = last_year
            if next_period != 12:
                next_2period = next_period + 1
                next_2period_last_year = last_year
            else:
                next_2period = 1
                next_2period_last_year = this_year
        else:
            next_period = 1
            next_2period = next_period + 1
            next_period_last_year = this_year
            next_2period_last_year = this_year

    if period == 'season':
        if this_period != 4:
            next_period = this_period + 1
            next_period_last_year = last_year
            if next_period != 4:
                next_2period = next_period + 1
                next_2period_last_year = last_year
            else:
                next_2period = 1
                next_2period_last_year = this_year
        else:
            next_period = 1
            next_2period = next_period + 1
            next_period_last_year = this_year
            next_2period_last_year = this_year

    return [next_period ,next_2period,next_period_last_year ,next_2period_last_year]

